package com.ljh.mboxvidoserver.system.service.impl;

import com.ljh.mboxvidoserver.system.dao.LoginRepository;
import com.ljh.mboxvidoserver.system.entity.LoginUser;
import com.ljh.mboxvidoserver.system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginRepository loginRepository;

    @Override
    public LoginUser login(LoginUser user) {
        if(user.getUsername()==null || "".equals(user.getUsername())){
            throw new RuntimeException("用户名不能为空");
        }
        if(user.getPassword()==null || "".equals(user.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        LoginUser loginUser = loginRepository.getByUsername(user.getUsername());
        if(loginUser==null){
            throw new RuntimeException("用户不存在");
        }
        if(loginUser.getPassword().equals(user.getPassword())){
            return loginUser;
        }else{
            throw new RuntimeException("密码错误");
        }
    }

    @Override
    public void loginOut() {
        System.out.println("登出");
    }
}
